package tuiLayer;
import controllerLayer.*;
import modelLayer.*;

import java.util.*;

public class PersonUI {
	private static PersonCTR personCtr;
	private static Scanner scanner = new Scanner(System.in);
	
	public static void menu(PersonCTR personCTR){
		personCtr = personCTR;
		boolean quit = false;
		while(!quit){
			printOptions();
			int input = prompt("Make your choice!");
			switch(input) {
				case 1:
					addFriend();
					break;
				case 2:
					int id = prompt("\nEnter the friend's id: ");
					personCtr.show(id);
					break;
				case 3:
					personCtr.showAll();
					break;
				case 4:
					updatePerson();
					break;	
				case 5:
					id = prompt("\nEnter the friend's id: ");
					personCtr.delete(id);
					break;
				case 6:
					quit = true;
					break;
				default:
					System.out.println("Sorry, but that's not a choice!");
					printOptions();
			}
		}
	}
	
	private static void printOptions(){
		System.out.println("***FriendsMenu***");
		System.out.println("1. AddFriend");
		System.out.println("2. Show a friend's info");
		System.out.println("3. Show all friends' info");
		System.out.println("4. Update a friend");
		System.out.println("5. Delete a friend");
		System.out.println("6. Back to MainMenu");
	}
	
	private static int prompt(String prompt){
		System.out.print(prompt + " ");
		int input = scanner.nextInt();
		return input;
	}
	
	private static String prompt2(String prompt) {
        System.out.print(prompt + " ");
        String input = scanner.next();
        return input.toLowerCase();
    }
	/**
	 * This method searches already existed person by his id.
	 * If the id is invalid the system prints a message : "Enter a valid id".
	 * If it is valid you have to type the new parameters:
	 * @param name
	 * @param address
	 * @param zip
	 * @param city
	 * @param phone
	 */
	private static void updatePerson(){
		int id;
		String name, address, zip, city, phone;
		id = prompt("Enter the id of the friend you want to change:");
		Person p = personCtr.findPerson(id);
		if(p==null)
		{
		    System.out.println("Enter valid id");
		  }
		  else
		  {
		name = prompt2("Enter the new name:");
		address = prompt2("Enter the new address:");
		zip = prompt2("Enter the new zipcode:");
		city = prompt2("Enter the new city:");
		phone = prompt2("Enter the new phonenumber:");
		
		personCtr.updatePerson(p, name, address, zip, city, phone);
          }
	}
	
	/**
	 * It creates and add the specific friend at the same time
	 */
	private static void addFriend(){
		String name, address, zip, city, phone;
		name = prompt2("Enter the name of the new friend:");
		address = prompt2("Enter the address of the new friend:");
		city = prompt2("Enter the city of the new friend:");
		zip = prompt2("Enter the zip of the new friend:");
		phone = prompt2("Enter the phonenumber of the new friend:");
		personCtr.createPerson(0, name, address, zip, city, phone);
	}
	
}
